﻿//******************************************************************************************
/**                                                                                       **
 ** 系统名称：项目管理系统                                                                **
 **                                                                                       **
 ** 模块名称：共通处理                                                                    **
 ** 文件名称：LogHelper.cs                                                                **
 ** 机能概述：输出各种日志                                                                **
 **                                                                                       **
 ** 作 成 者：                                                                            **
 **                                                                                       **
 ** 变更履历　：                                                                          **
 ** NO      变更日      变更者      变更内容                                              **
 **  1      2008/06/21  浦簡冬        新建                                                **
 **  2                                                                                    **
 */
//******************************************************************************************
using System;
using log4net;
using System.IO;
using System.Web;
using System.Text;
using System.Collections;
using System.Configuration;
using SaleStat.COM.Constant;


namespace SaleStat.COM.LogHelper
{
    public class LogHelper
    {
        #region 常量定义
        // IP地址的长度
        private const int LEN_IP_ADDRESS = 15;

        // 运行中的类名的长度
        private const int LEN_CLASSNAME = 100;
        #endregion

        #region 输出消息日志
        /// <summary>
        /// 输出消息日志
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="strPageId"></param>
        /// <param name="strMessage"></param>
        public static void f_Info(CommonConstant.OPERATIONS operation, string strPageId, string strMessage)
        {
            ILog logger = LogManager.GetLogger(strPageId);

            // 如果消息日志可用
            if (logger.IsInfoEnabled)
            {
                logger.Info(f_GetLogMsg(Convert.ToInt16(operation), strPageId, strMessage));
            }
        }
        #endregion

        #region 输出错误日志
        /// <summary>
        /// 输出错误日志
        /// </summary>
        /// <param name="strUserName"></param>
        /// <param name="operation"></param>
        /// <param name="strPageId"></param>
        /// <param name="strMessage"></param>
        public static void f_Error(CommonConstant.OPERATIONS operation, string strPageId, string strMessage)
        {
            ILog logger = LogManager.GetLogger(strPageId);

            // 如果错误日志可用
            if (logger.IsErrorEnabled)
            {
                logger.Error(f_GetLogMsg(Convert.ToInt16(operation), strPageId, strMessage));
            }
        }
        #endregion

        #region 取得输出日志用的信息
        /// <summary>
        /// 取得输出日志用的信息
        /// </summary>
        /// <param name="intOperation"></param>
        /// <param name="strPageId"></param>
        /// <param name="strMessage"></param>
        /// <returns></returns>
        private static string f_GetLogMsg(int intOperation, string strPageId, string strMessage)
        {
            HttpContext ctx = HttpContext.Current;

            string strUserName = ctx.Session[SessionKeyConstant.SESSION_USERNAME].ToString();
            string strUserID = ctx.Session[SessionKeyConstant.SESSION_USERID ].ToString();
            string strIpAddress = ctx.Session[SessionKeyConstant.SESSION_IPADDRESS].ToString();
            // 取得处理后的IP地址
            strIpAddress = f_AddSpace(strIpAddress, LEN_IP_ADDRESS);

            //日志格式内容
            string logMsg = "";
            logMsg += "UserName=" + strUserName + ",";
            logMsg += "UserID=" + strUserID + ",";
            logMsg += "IP=" + strIpAddress + ",";
            logMsg += "PageId=" + strPageId + ",";
            logMsg += "\"MSG=" + strMessage + "\"";
            return logMsg;
        }
        #endregion

        #region 对长度不足的对象补充空格
        /// <summary>
        /// 对长度不足的对象补充空格
        /// </summary>
        /// <param name="target"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private static string f_AddSpace(string target, int length)
        {
            string result = "";
            result += target;
            while (result.Length < length)
            {
                result += " ";
            }
            return result.Substring(0, length);
        }
        #endregion

        //#region 日志的初期设定
        ///// <summary>
        ///// 日志的初期设定
        ///// </summary>
        //public static void f_InitLog()
        //{
        //    string filePath = HttpContext.Current.Request.MapPath(ConfigurationManager.AppSettings["filePath"].ToString());

        //    log4net.Config.XmlConfigurator.Configure(new FileInfo(filePath + @"log4netConfig.xml"));
        //}
        //#endregion
    }
}
